package com.amikomgamedev.labirinhantu.state;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.FadeInModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.Server_Data;

public class State_Game_Win
	extends
	    State_Game_Over
{
    private float SPEED = 0.5f;

    private boolean onAreaActive	= false;
    
    protected void create_Game_Win()
    {
	onAreaActive	= false;
	
	grafik.attachObjGameWin();
//	grafik.attachMenuLevelGembok();
	
	for (int i = 0; i < grafik.lst_Game_Win_Tulang.size(); i++)
	{
	    if (i == 0)
        	    m_Scene.registerUpdateHandler(new TimerHandler((i + 1) *SPEED, new ITimerCallback()
        	    {
        	        public void onTimePassed(TimerHandler pTimerHandler)
        	        {
        		    if (0 == grafik.lst_Game_Win_Tulang.size() - 1)
        			onAreaActive	= true;
        		    
        	            grafik.lst_Game_Win_Tulang.get(0).setVisible(true);
        	            grafik.lst_Game_Win_Tulang.get(0).registerEntityModifier(new ScaleModifier(SPEED, 10f, 1f));
        	            grafik.lst_Game_Win_Tulang.get(0).registerEntityModifier(new FadeInModifier(SPEED));
        	            sound.playGameSfx(SFX_GAME_TULANG);
        	        }
        	    }));
	    else if (i == 1)
        	    m_Scene.registerUpdateHandler(new TimerHandler((i + 1) *SPEED, new ITimerCallback()
        	    {
        	        public void onTimePassed(TimerHandler pTimerHandler)
        	        {
        		    if (1 == grafik.lst_Game_Win_Tulang.size() - 1)
        			onAreaActive	= true;
        		    
        	            grafik.lst_Game_Win_Tulang.get(1).setVisible(true);
        	            grafik.lst_Game_Win_Tulang.get(1).registerEntityModifier(new ScaleModifier(SPEED, 10f, 1f));
        	            grafik.lst_Game_Win_Tulang.get(1).registerEntityModifier(new FadeInModifier(SPEED));
        	            sound.playGameSfx(SFX_GAME_TULANG);
        	        }
        	    }));
	    else if (i == 2)
        	    m_Scene.registerUpdateHandler(new TimerHandler((i + 1) *SPEED, new ITimerCallback()
        	    {
        	        public void onTimePassed(TimerHandler pTimerHandler)
        	        {
        		    if (2 == grafik.lst_Game_Win_Tulang.size() - 1)
        			onAreaActive	= true;
        		    
        	            grafik.lst_Game_Win_Tulang.get(2).setVisible(true);
        	            grafik.lst_Game_Win_Tulang.get(2).registerEntityModifier(new ScaleModifier(SPEED, 10f, 1f));
        	            grafik.lst_Game_Win_Tulang.get(2).registerEntityModifier(new FadeInModifier(SPEED));
        	            sound.playGameSfx(SFX_GAME_TULANG);
        	        }
        	    }));
	    
//	    if (i == grafik.lst_Game_Win_Tulang.size() - 1)
//		onAreaActive	= true;
//	    {
//		onAreaActive	= true;
//		for (int j = 0; j < grafik.lst_Game_Win_Btn.size(); j++)
//		{
//			m_Hud.registerTouchArea(grafik.lst_Game_Win_Btn.get(i));
//		}
//	    }
	}
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
	if (State_Menu_Current == STATE_GAME_WIN)
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK)
	    {
		grafik.detachGameWin();
		detachGame();
	    }
	}

	return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onAreaTouched(
	    TouchEvent pSceneTouchEvent,
	    ITouchArea pTouchArea,
	    float pTouchAreaLocalX,
	    float pTouchAreaLocalY)
    {
	if (State_Menu_Current == STATE_GAME_WIN)
	{
	    if (onAreaActive)
	    if (pSceneTouchEvent.isActionUp())
		for (int i = 0; i < grafik.lst_Game_Win_Btn.size(); i++)
		{
		    if (pTouchArea.equals(grafik.lst_Game_Win_Btn.get(i)))
		    {
			grafik.detachGameWin();

			if (i == 0)
			    detachGame();
			else if (i == 1)
			    restartGame();
			else if (i == 2)
			{
			    Server_Data.setMap(Server_Data.getMap() + 1);
			    restartGame();
			}
		    }
		}
	}

	return super.onAreaTouched(
		pSceneTouchEvent,
		pTouchArea,
		pTouchAreaLocalX,
		pTouchAreaLocalY);
    }
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_GAME_WIN)
		sound.stopGameBgm(Server_Data.getStage());
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_GAME_WIN)
		sound.playGameBgm(Server_Data.getStage());

	    super.onResumeGame();
	}
}
